package org.crazyit.activiti_d3._3_UpdateUser;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.User;

import java.util.UUID;

public class UpdateUser {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到身份服务组件实例
        IdentityService identityService = engine.getIdentityService();

        //3. 创建用户
        String id = UUID.randomUUID().toString();
        createUser(identityService,id,"angus","young","yangenxiong@163.com","abc");

        //4. 查询用户
        User user = identityService.createUserQuery().userId(id).singleResult();
        System.out.println("修改前邮箱: " + user.getEmail());

        //5. 修改用户邮箱
        user.setEmail("abc@163.com");
        identityService.saveUser(user);

        //6. 查询修改结果
        User user1 = identityService.createUserQuery().userId(id).singleResult();
        System.out.println("修改后邮箱: " + user1.getEmail());

    }

    public static void createUser(IdentityService identityService,
                                  String id,
                                  String firstName,
                                  String lastName,
                                  String email,
                                  String password) {

        //1. 使用newUser 方法创建User 实例
        User user = identityService.newUser(id);

        //2. 设置用户的各个属性
        user.setFirstName(firstName);
        user.setLastName(lastName);
        user.setEmail(email);
        user.setPassword(password);

        //3. 使用saveUser 方法保存用户
        identityService.saveUser(user);

    }

}
